package com.zy.dp.proxy.example3;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy implements InvocationHandler
{

	private OrderApi order = null;

	public OrderApi getProxyInterface(OrderApi order)
	{

		this.order = order;

		OrderApi orderApi = (OrderApi) Proxy.newProxyInstance(order.getClass()
				.getClassLoader(), order.getClass().getInterfaces(), this);

		return orderApi;

	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable
	{

		if (method.getName().startsWith("set"))
		{
			if (order.getOrderUser() != null
					&& order.getOrderUser().equals(args[1]))
			{
				return method.invoke(order, args);
			}
			else
			{
				System.out.println("对不起，" + args[1] + "，您无权修改本订单中的数据");
			}
		}
		else
		{
			return method.invoke(order, args);
		}

		return null;

	}

}
